// 引入路由组件router并使用
import VueRouter from "vue-router";
import Vue from "vue";
Vue.use(VueRouter)

// 该段代码不需要记，理解即可
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

const routes = [
    {path:'/' , redirect: '/layout'},
    {
        path:'/layout' , 
        component: () => import('@/views/layout/Layout.vue'),
        children:[
            {path:'' ,
             component: () => import('@/views/layout/home/Home.vue') ,
             meta:{
                 title:'首页',
                 icon:'el-icon-s-home'
             },
             name:'Home'
            },
            {path:'user' , 
             component: () => import('@/views/layout/user/User.vue'),
             meta:{
                title:'用户管理',
                icon:'el-icon-s-custom'
             },
             name:'User'    
            },
            {path:'info' ,
             component: () => import('@/views/layout/info/Infomanage.vue'),
             meta:{
                title:'信息管理',
                icon:'el-icon-s-finance'
             },
             name:'Info',

             children:[
                 {path:'' , 
                  component: () => import('@/views/layout/info/Myinfo.vue'),
                  meta:{
                    title:'个人信息',
                    icon:'el-icon-s-release'
                  },
                  name:'Myinfo'   
                 }
             ],
         
            },
   
           
        ]
    },
    {
        path:'/login',
        component: () => import('@/views/login/Login.vue')
    },
]


// 创建路由实例对象
const router = new VueRouter({
    mode:'history',
    routes:routes
})
// 导入store实例
import store from '@/store';
import {pressApi} from '@/utils/https.js'
// 设置路由全局前置守卫
import { getCookie } from "@/utils/cookie";
router.beforeEach((to,from , next)=>{
    // 获取token
    let token = getCookie('token');
    // console.log(getCookie('token'));
    if(token){
        if(to.path == '/login'){
            next('/')
        }else{
            let role = localStorage.getItem('role')

            if(store.state.routes.length == 0){
                pressApi({role:role}).then((res) =>{
                    console.log(res);
                    res.data.forEach(item => {
                        item.component = () => import(`@/views/layout/vaction/${item.name}.vue`)
                        routes[1].children.push(item)
                    });
                    console.log(routes);
                    console.log(store);
                    router.addRoutes(routes)
                    store.dispatch('setRoutesAsync', routes)
        
                    next({ ...to,replace:true})
                })
            }else{
                next()
            }
         
        }

       
        
    }else if(!token && to.path != '/login'){
        next('/login')
    }else{
        next()
    }
   
})



export default router
